﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using ShivamEMR.Model.Domain.AccountsDomain;
using ShivamEMR.Model.Domain.AssetsDomain;
using ShivamEMR.Model.Domain.CommonDomain;
using ShivamEMR.Model.Domain.CompanyDomain;
using ShivamEMR.Model.Domain.ContactsDomain;
using ShivamEMR.Model.Domain.CRMDomain;
using ShivamEMR.Model.Domain.DoctorsDomain;
using ShivamEMR.Model.Domain.InventoryDomain;
using ShivamEMR.Model.Domain.LocationDomain;
using ShivamEMR.Model.Domain.MedicalDomain;
using ShivamEMR.Model.Domain.PatientDomain;
using ShivamEMR.Model.Domain.PersonDomain;
using ShivamEMR.Model.Domain.PharmacyDomain;
using ShivamEMR.Model.Domain.ServicesDomain;
using ShivamEMR.Model.Domain.UIDomain;
using ShivamEMR.Model.Domain.UserDomain;

namespace ShivamEMR.Model.Domain
{
  public  class ShivamEMRContext  :DbContext
    {


      public ShivamEMRContext()
          : base("Data Source=.;Initial Catalog=ShivamEMR.Data.Context; Integrated Security=True; MultipleActiveResultSets=true")

          // : base("Data Source=.\\SqlExpress;Initial Catalog=EMRDEMODB;User ID=sa;Password=ideas; Integrated Security=True; MultipleActiveResultSets=true")
          // : base("Data Source=103.247.149.37;Initial Catalog=ShivamEMRDB;User ID=dushyant;Password=happy@123; MultipleActiveResultSets=true")
      {

      }
    
      #region AccountsModule
      //Accounts Module
      public DbSet<Account> Accounts { get; set; }
      public DbSet<AccountGroup> AccountGroups { get; set; }
      public DbSet<AccountingRule> AccountingRules { get; set; }
      public DbSet<AccountNature> AccountNatures { get; set; }
      public DbSet<AccountStatus> AccountStatus { get; set; }
      public DbSet<AccountType> AccountTypes { get; set; }
      public DbSet<Depreciation> Depreciations { get; set; }
      public DbSet<AccountsEntry> AccountingEntries { get; set; }
      public DbSet<AccountsPicture> AccountsPictures { get; set; }
      public DbSet<FinancialYear> FinancialYears { get; set; }
      public DbSet<Money> Monies { get; set; }
      public DbSet<AccountsPostingType> AccountsPostingTypes { get; set; }
      public DbSet<Payment> Payments { get; set; }
      public DbSet<PaymentType> PaymentTypes { get; set; }
      public DbSet<PaymentStatus> PaymentStatus { get; set; }
      public DbSet<PaymentVoucher> PaymentVouchers { get; set; }
      public DbSet<PurchaseVoucher> PurchaseVouchers { get; set; }
      public DbSet<ReceiptVoucher> ReceiptVouchers { get; set; }
      public DbSet<SalesVoucher> SalesVouchers { get; set; }
      public DbSet<Tax> Taxes { get; set; }
      public DbSet<TaxInformation> TaxInformations { get; set; }
      public DbSet<AccountsTransaction> AccountsTransactions { get; set; }
      public DbSet<AccountsTransactionType> AccountsTransactionTypes { get; set; }
      public DbSet<AccountsVoucher> AccountsVouchers { get; set; }
      public DbSet<AccountsVoucherType> AccountsVoucherTypes { get; set; }
      public DbSet<TaxType> TaxTypes { get; set; }
      #endregion
     
      #region AssetModule
      //Asset Module
      public DbSet<Asset> Assets { get; set; }
      public DbSet<AssetGroup> AssetGroups { get; set; }
      public DbSet<AssetStatus> AssetStatus { get; set; }
      public DbSet<AssetPicture> AssetPictures { get; set; } 
      #endregion

      #region CommonModule
      //Common Module
      public DbSet<Alert> Alerts { get; set; }
      public DbSet<DataDictionary> DataDictionary { get; set; }
      public DbSet<DataDictionaryItemStatus> DataDictionaryItemStatus { get; set; }
      public DbSet<Document> Documents { get; set; }
      public DbSet<Unit> Units { get; set; } 
      #endregion

      #region CompanyModule
      //company module
      public DbSet<Branch> Branches { get; set; }
      public DbSet<BranchGroup> BranchGroups { get; set; }
      public DbSet<Company> Companies { get; set; }
      public DbSet<CompanyGroup> CompanyGroups { get; set; }
      public DbSet<CompanyPicture> CompanyPictures { get; set; }
      public DbSet<Department> Departments { get; set; }
      public DbSet<Designation> Designations { get; set; }
      public DbSet<Employee> Employees { get; set; }
      public DbSet<EmployeeGroup> EmployeeGroups { get; set; }
      public DbSet<EmployeeSchedule> EmployeeSchedules { get; set; }
      public DbSet<EmployeeStatus> EmployeeStatus { get; set; }
      public DbSet<Schedule> Schedules { get; set; }
      public DbSet<ScheduleStatus> ScheduleStatus { get; set; } 
      #endregion    

      #region ContactModule
      //Contact Module
      public DbSet<Address> Addresses { get; set; }
      public DbSet<AddressType> AddressTypes { get; set; }
      public DbSet<City> Cities { get; set; }
      public DbSet<Contact> Contacts { get; set; }
      public DbSet<ContactPicture> ContactPictures { get; set; }
      public DbSet<ContactType> ContactTypes { get; set; }
      public DbSet<Country> Countries { get; set; }
      public DbSet<CultureAndRegion> CultureAndRegions { get; set; }
      public DbSet<Nationality> Nationalities { get; set; }
      public DbSet<Territory> Territories { get; set; } 
      #endregion

      #region CRMModule
      //CRM Module
      public DbSet<AnswerDetail> AnswerDetails { get; set; }
      public DbSet<Appointment> Appointments { get; set; }
      public DbSet<AppointmentGroup> AppointmentGroups { get; set; }
      public DbSet<AppointmentStatus> AppointmentStatus { get; set; }
      public DbSet<Assignment> Assignments { get; set; }
      public DbSet<AssignmentGroup> AssignmentGroups { get; set; }
      public DbSet<AssignmentStatus> AssignmentStatus { get; set; }

      public DbSet<Event> Events { get; set; }
      public DbSet<Interaction> Interactions { get; set; }
      public DbSet<InteractionGroup> InteractionGroups { get; set; }
      public DbSet<Message> Messages { get; set; }
      public DbSet<MessageGroup> MessageGroups { get; set; }
      public DbSet<MessageStatus> MessageStatus { get; set; }
      public DbSet<MessageType> MessageTypes { get; set; }
      public DbSet<Question> Questions { get; set; }
      public DbSet<QuestionAnswer> QuestionAnswers { get; set; }
      public DbSet<QuestionGroup> QuestionGroups { get; set; }
      public DbSet<QuestionOption> QuestionOptions { get; set; }
      public DbSet<QuestionStatus> QuestionStatus { get; set; }
      public DbSet<QuestionType> QuestionTypes { get; set; }
      public DbSet<Venue> Venues { get; set; } 
      #endregion

      #region DoctorModule
      //Doctor Module
      public DbSet<Doctor> Doctors { get; set; }
      public DbSet<Specialty> Specialties { get; set; } 
      #endregion

      #region InventoryModule
      //Inventory Module
      public DbSet<Batch> InventoryBatches { get; set; }
      public DbSet<BatchStatus> InventoryBatchStatus { get; set; }
      public DbSet<BatchType> InventoryBatchTypes { get; set; }
      public DbSet<InventoryEntry> InventoryEntries { get; set; }
      public DbSet<InventoryPostingType> InventoryPostingTypes { get; set; }
      public DbSet<InventoryTransaction> InventoryTransactions { get; set; }
      public DbSet<InventoryVoucher> InventoryVouchers { get; set; }
      public DbSet<InventoryVoucherType> InventoryVoucherTypes { get; set; }
      public DbSet<Invoice> Invoices { get; set; }
      public DbSet<InvoiceGroup> InvoiceGroups { get; set; }
      public DbSet<InvoiceItem> InvoiceItems { get; set; }
      public DbSet<InvoiceStatus> InvoiceStatus { get; set; }
      public DbSet<InvoiceType>  InvoiceType{ get; set; }
      public DbSet<Item> Items { get; set; }
      public DbSet<ItemGroup> ItemGroups { get; set; }
      public DbSet<ItemPicture> ItemPictures { get; set; }
      public DbSet<ItemSale> ItemSales { get; set; }
      public DbSet<ItemStatus> ItemStatus { get; set; }
      public DbSet<ItemType> ItemType { get; set; }
      public DbSet<Manufacturer> Manufacturers { get; set; }
      public DbSet<ManufacturerGroup> ManufacturerGroups { get; set; }
      public DbSet<Order> Orders { get; set; }
      public DbSet<OrderGroup> OrderGroups { get; set; }
      public DbSet<OrderItem> OrderItems { get; set; }
      public DbSet<OrderStatus> OrderStatus { get; set; }
      public DbSet<OrderType> OrderTypes { get; set; }
      public DbSet<Packing> Packings { get; set; }
      public DbSet<PackingType> PackingTypes { get; set; }      
      public DbSet<Price> Prices { get; set; }
      public DbSet<PriceType> PriceTypes { get; set; }
      public DbSet<Supplier> Suppliers { get; set; }
      public DbSet<SupplierGroup> SupplierGroups { get; set; }
    
      public DbSet<InventoryTransactionType> InventoryTransactionTypes { get; set; }
      
     
      #endregion

      #region LocationModule
   //   Location Module
      public DbSet<Location> Locations { get; set; }
      public DbSet<LocationGroup> LocationGroups { get; set; }
      public DbSet<LocationPicture> LocationPictures { get; set; } 
      #endregion

      #region MedicalModule
      //Medical Module
      public DbSet<Disease> Diseases { get; set; }
      public DbSet<DiseaseCategory> DiseaseCategories { get; set; }
      public DbSet<DiseaseGroup> DiseaseGroups { get; set; }
      public DbSet<Radiology> Radiologies { get; set; }
      public DbSet<RadiologyAttachment> RadiologyAttachments { get; set; }
      public DbSet<RadiologyGroup> RadiologyGroups { get; set; }
      public DbSet<RadiologyItem> RadiologyItems { get; set; }
      public DbSet<Test> Tests { get; set; }
      public DbSet<TestGroup> TestGroups { get; set; }
      public DbSet<TestItem> TestItems { get; set; }
      public DbSet<TreatmentType> TreatmentTypes { get; set; }
      #endregion

      #region PatientModule
      //Patient Module
      public DbSet<Admission> Admissions { get; set; }
      public DbSet<AdmissionStatus> AdmissionStatus { get; set; }
      public DbSet<AdmissionType> AdmissionTypes { get; set; }
      public DbSet<Consultation> Consultations { get; set; }
      public DbSet<Diagnosis> Diagnosis { get; set; }
      public DbSet<DiagnosisStatus> DiagnosisStatus { get; set; }
      public DbSet<DiagnosticsItem> DiagnosticItems { get; set; }
      public DbSet<Insurance> Insurances { get; set; }
      public DbSet<InsuranceCategory> InsuranceCategories { get; set; }
      public DbSet<InsurancePlan> InsurancePlans { get; set; }
      public DbSet<InsuranceStatus> InsuranceStatus { get; set; }
      public DbSet<Medication> Medications { get; set; }
      public DbSet<MedicationStatus> MedicationStatus { get; set; }
      public DbSet<Patient> Patients { get; set; }
      public DbSet<PatientAllergy> PatientAllergies { get; set; }
      public DbSet<PatientAllergyGroup> PatientAllergyGroups { get; set; }
      public DbSet<PatientGroup> PatientGroups { get; set; }
      public DbSet<PatientInformation> PatientInformations { get; set; }
      public DbSet<PatientRadiology> PatientRadiologies { get; set; }
      public DbSet<PatientRadiologyAttachments> PatientRadiologyAttachments { get; set; }
      public DbSet<PatientRadiologyPicture> PatientRadiologyPictures { get; set; }
      public DbSet<PatientRepresentative> PatientRepresentatives { get; set; }
      public DbSet<PatientService> PatientServices { get; set; }
      public DbSet<PatientTest> PatientTests { get; set; }
      public DbSet<PatientTestPicture> PatientTestPictures { get; set; }
      public DbSet<PatientTreatment> PatientTreatments { get; set; }
      public DbSet<TreatmentStatus> TreatmentStatus { get; set; }

      #endregion           

      #region PersonModule
      //Person Module
      public DbSet<BloodGroup> BloodGroups { get; set; }
      public DbSet<MaritalStatus> MaritalStatus { get; set; }
      public DbSet<Person> People { get; set; }
      public DbSet<PersonAccount> PersonAccounts { get; set; }
      public DbSet<PersonPicture> PersonPictures { get; set; }
      public DbSet<PersonRole> PersonRoles { get; set; }
      public DbSet<PersonStage> PersonStages { get; set; }
      public DbSet<PersonStatus> PersonStatus { get; set; }
      public DbSet<Sex> Sexes { get; set; } 
      #endregion

      #region PharmacyModule
      //Pharmacy Module
      public DbSet<Dosage> Dosage { get; set; }
      public DbSet<Drug> Drugs { get; set; }
      public DbSet<DrugBrand> DrugBrands { get; set; }
      public DbSet<DrugCategory> DrugCategories { get; set; }
      public DbSet<DrugClass> DrugClasses { get; set; }
      public DbSet<DrugContraIndication> ContraIndications { get; set; }
      public DbSet<DrugGeneric> DrugGenerics { get; set; }
      public DbSet<DrugGroup> DrugGroups { get; set; }
      public DbSet<DrugInteraction> DrugInteractions { get; set; }
      public DbSet<DrugItem> DrugItems { get; set; }
      public DbSet<DrugPacking> DrugPackings { get; set; }
      public DbSet<DrugPicture> DrugPictures { get; set; } 
      #endregion

      #region ServicesModule
      public DbSet<EMRService>  EMRServices{ get; set; }
      public DbSet<EMRServiceGroup> EMRServiceGroups { get; set; }
      public DbSet<EMRServicePackage> ServicePackages { get; set; }
      public DbSet<EMRServicePackageStatus> PackageStatus { get; set; }
      public DbSet<EMRServicePackageType> PackageType { get; set; }
      public DbSet<EMRServiceScheme> ServiceSchemes { get; set; }
      public DbSet<EMRServiceSchemeType> SchemeTypes { get; set; }
      public DbSet<EMRServiceType> ServiceTypes { get; set; }
      public DbSet<ServiceItem> ServiceItems { get; set; }
      public DbSet<ServiceItemStatus> ServiceItemStatus { get; set; }
      public DbSet<ServicePicture> ServicePictures { get; set; }
      public DbSet<ServicePrice> ServicePrices { get; set; }
      #endregion

      #region UIModules
      //UIModule Module
     // public DbSet<Menu> Menus { get; set; }
     // public DbSet<MenuGroup> MenuGroups { get; set; }
      public DbSet<UIMetaData> UIMetaDatas { get; set; }
      public DbSet<UIMetaDataGroup> UIMetaDataGroups { get; set; }
      public DbSet<UIMetaDataStatus> UIMetaDataStatus { get; set; }
      public DbSet<UIMetaDataType> UIDataTypes { get; set; }
      public DbSet<UIStyle> UIStyles { get; set; } 
      #endregion

      #region UserModule
      //User Module
      public DbSet<AccessPermission> AccessPermissions { get; set; }
      public DbSet<User> Users { get; set; }
      public DbSet<UserLog> UserLog { get; set; }
      public DbSet<UserRight> UserRights { get; set; }
      public DbSet<UserRightType> UserRightTypes { get; set; }
      public DbSet<UserRole> UserRoles { get; set; }
      public DbSet<UserTask> UserTasks { get; set; }     
      #endregion 

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
            //base.OnModelCreating(modelBuilder);
           modelBuilder.Entity<Person>().ToTable("People");
           modelBuilder.Entity<Patient>().ToTable("Patients");
           modelBuilder.Entity<Employee>().ToTable("Employee");
           modelBuilder.Entity<Doctor>().ToTable("Doctors");
           modelBuilder.Entity<PatientRepresentative>().ToTable("PatientRepresentatives");
           modelBuilder.Entity<Item>().ToTable("Items");
           modelBuilder.Entity<TestItem>().ToTable("TestItems");
           modelBuilder.Entity<DrugItem>().ToTable("DrugItems");
           modelBuilder.Entity<User>().ToTable("Users");
           modelBuilder.Entity<DiagnosticsItem>().ToTable("DiagnosticItems");
           modelBuilder.Entity<RadiologyItem>().ToTable("RadiologyItems");
           modelBuilder.Entity<ServiceItem>().ToTable("ServiceItems");

           modelBuilder.Entity<AccountsVoucher>().ToTable("AccountsVouchers");
           modelBuilder.Entity<PaymentVoucher>().ToTable("PaymentVouchers");
           modelBuilder.Entity<PurchaseVoucher>().ToTable("PurchaseVouchers");
           modelBuilder.Entity<ReceiptVoucher>().ToTable("ReceiptVouchers");
           modelBuilder.Entity<SalesVoucher>().ToTable("SalesVoucher");

           modelBuilder.Entity<MaritalStatus>().HasMany(m => m.Persons).WithOptional(p => p.MaritalStatus).HasForeignKey(p => p.MaritalSatusID);

       //   modelBuilder.Entity<Account>().HasOptional(a => a.Taxes).WithMany().Map(m => { m.ToTable("AccountTaxes"); m.MapLeftKey("AccountID"); m.MapRightKey("TaxID"); });
         //  modelBuilder.Entity<Account>().HasOptional(a => a.Taxes).WithMany();

           modelBuilder.Entity<User>().HasMany(parent => parent.Rights).WithMany();

           //modelBuilder.Entity<User>().HasMany(usr => usr.DashboardMenus).WithMany(m => m.Users);
           //modelBuilder.Entity<Menu>().HasRequired(m => m.Right).WithOptional(r => r.Menu).WillCascadeOnDelete(false);
       //    modelBuilder.Entity<UserRight>().HasOptional(r => r.MenuItem).WithOptionalPrincipal();
          // modelBuilder.Entity<Menu>().HasRequired(m => m.Right).WithRequiredPrincipal();
           modelBuilder.Entity<UserRole>().HasMany(ur => ur.Rights).WithMany(r => r.UserRoles);

          modelBuilder.Entity<Country>().HasMany(c => c.Cities).WithRequired(c => c.Country).HasForeignKey(k=>k.CountryID).WillCascadeOnDelete(false);
          modelBuilder.Entity<Country>().HasMany(c => c.Territories).WithRequired(c => c.Country).HasForeignKey(k => k.CountryID).WillCascadeOnDelete(false);
          //modelBuilder.Entity<Territory>().HasMany(t => t.Cities).WithOptional(c => c.Territory).HasForeignKey(k => k.TerriotyID).WillCascadeOnDelete(false);
           modelBuilder.Entity<City>().HasOptional(c => c.Territory).WithMany(t => t.Cities).WillCascadeOnDelete(false);
         // modelBuilder.Entity<City>().HasRequired(c => c.Country).WithMany(c => c.Cities).HasForeignKey(k => k.CountryID).WillCascadeOnDelete();
           modelBuilder.Entity<Branch>().HasOptional(b => b.Group).WithMany(b => b.Branches).WillCascadeOnDelete(false);
                   
           modelBuilder.Entity<Item>().HasOptional(i => i.Picture).WithOptionalPrincipal();

           modelBuilder.Entity<Location>().HasOptional(l => l.Picture).WithOptionalPrincipal();

           modelBuilder.Entity<Test>().HasOptional(t => t.TestPicture).WithOptionalPrincipal();

           modelBuilder.Entity<Radiology>().HasOptional(r => r.Attachment).WithOptionalPrincipal();

          modelBuilder.Entity<Insurance>().HasOptional(i => i.Plan).WithOptionalPrincipal();

           modelBuilder.Entity<PatientTest>().HasOptional(pt => pt.Picture).WithOptionalPrincipal();

           modelBuilder.Entity<EMRService>().HasOptional(es => es.Picture).WithOptionalPrincipal();

           modelBuilder.Entity<PatientTreatment>().HasRequired(pt => pt.Patient).WithMany(p => p.Treatments).HasForeignKey(p=>p.PatientID).WillCascadeOnDelete(false);

           modelBuilder.Entity<Diagnosis>().HasRequired(d => d.Patient).WithMany(p => p.Diagnostics).HasForeignKey(d => d.PatientID).WillCascadeOnDelete(false);

           modelBuilder.Entity<Admission>().HasRequired(a => a.Patient).WithMany(p => p.Admissions).HasForeignKey(a => a.PatientID).WillCascadeOnDelete(false);

           modelBuilder.Entity<Person>().HasOptional(p => p.Account).WithOptionalPrincipal();

           modelBuilder.Entity<DrugBrand>().HasOptional(db => db.Picture).WithOptionalPrincipal();
         
      }
    }
}
